package com.wbao.dianniu.tabstrip.fragments;

import java.io.File;
import java.util.ArrayList;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.listener.IHeadImageListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.HeadImageManger;
import com.wbao.dianniu.ui.HeadInfoActivity;
import com.wbao.dianniu.ui.HeadInfoViewActivity;
import com.wbao.dianniu.ui.IndustryAuthActivity;
import com.wbao.dianniu.ui.MyAmActivity;
import com.wbao.dianniu.ui.MyCollectionActivity;
import com.wbao.dianniu.ui.MyQAnswerActivity;
import com.wbao.dianniu.ui.PerfectInfoActivity;
import com.wbao.dianniu.ui.SettingActivity;
import com.wbao.dianniu.utils.GlideCircleTransform;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 我
 * 
 * @author yejianjun
 * 
 */
public class FourthFragment extends Fragment implements OnClickListener,IHeadImageListener {

	/** 头像 */
	private ImageView headIV;
	/**	个人资料信息	*/
	private RelativeLayout headInfoRl;
	/** 编辑资料 */
	private Button persionInfoBtn;
	/** 完善资料 */
	private RelativeLayout prefectInfoRl;
	/** 行业认证 */
	private Button industryBtn;
	/** 我的问答 */
	private RelativeLayout rl_myquestion;
	/** 与我相关 */
	private RelativeLayout rl_anonymous;
	/** 设置 */
	private RelativeLayout rl_setting;
	/** 我的收藏 */
	private RelativeLayout rl_mycollection;
	private TextView nickNameTV;
	private TextView labelTV;
	private TextView signateTV;
	private Dialog dialog;

	public static FourthFragment instance() {
		FourthFragment view = new FourthFragment();
		//在该Fragment的构造函数中注册mTouchListener的回调
		return view;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		HeadImageManger.getInstance().addListener(this);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		HeadImageManger.getInstance().removeListener(this);
	}
	
	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fourth_fragment, null);
		initview(view);
		return view;
	}

	private void initview(View view) {
		headIV = (ImageView) view.findViewById(R.id.headIcon);
		headInfoRl = (RelativeLayout) view.findViewById(R.id.head_info_relative);
		persionInfoBtn = (Button) view.findViewById(R.id.persion_info);
		industryBtn = (Button) view.findViewById(R.id.industry_auth);
		prefectInfoRl = (RelativeLayout) view.findViewById(R.id.rl_prefect_info);
		rl_myquestion = (RelativeLayout) view.findViewById(R.id.rl_myquestion);
		rl_anonymous = (RelativeLayout) view.findViewById(R.id.rl_anonymous);
		rl_setting = (RelativeLayout) view.findViewById(R.id.rl_sets);
		rl_mycollection = (RelativeLayout) view.findViewById(R.id.rl_collection);
		nickNameTV = (TextView) view.findViewById(R.id.dn_name);
		labelTV =  (TextView) view.findViewById(R.id.label_id);
		signateTV =  (TextView) view.findViewById(R.id.dn_signature);
		PictureDownloadUtils.displayHeadImage(getContext(),headIV, GlobalContext.headPic);
//		HeadPresenter presenter = new HeadPresenter(getActivity());
//		presenter.showHeadPic(getContext(), GlobalContext.headPic, headIV);
		
		nickNameTV.setText(GlobalContext.niceName == null ? "" : GlobalContext.niceName);
		labelTV.setText(GlobalContext.label==null ? "": GlobalContext.label);
		signateTV.setText(GlobalContext.signate == null ? "": "签名：" + GlobalContext.signate);
		if(GlobalContext.isPerfectData){
			prefectInfoRl.setVisibility(View.GONE);
		}else{
			prefectInfoRl.setVisibility(View.VISIBLE);
		}  
		
		headIV.setOnClickListener(this);
		headInfoRl.setOnClickListener(this);
		persionInfoBtn.setOnClickListener(this);
		industryBtn.setOnClickListener(this);
		prefectInfoRl.setOnClickListener(this);
		rl_myquestion.setOnClickListener(this);
		rl_anonymous.setOnClickListener(this);
		rl_setting.setOnClickListener(this);
		rl_mycollection.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.headIcon:
			/** 点击放大图片 */
//			Intent intent = new Intent();
//			intent.putExtra(GlobalContext.BUNDLE_HEADPIC, GlobalContext.headPic);
//			intent.setClass(getActivity(), HeadInfoViewActivity.class);
//			getActivity().startActivity(intent);
			ArrayList<String> urls2 = new ArrayList<>();
			urls2.add(GlobalContext.cdndownUrl + GlobalContext.headPic);
			Utils.imageBrower(getContext(), 0, urls2);
			break;
		case R.id.persion_info:
		case R.id.head_info_relative:
			/** 跳转到完善资料 */
			intent(PerfectInfoActivity.class);
			break;
		case R.id.industry_auth:
			if(!GlobalContext.isPerfectData){
				showAuthLevel("请先完善资料！");
			}else{
				/** 跳转到行业认证 */
				intent(IndustryAuthActivity.class);
			}
			break;
		case R.id.rl_prefect_info:
			/** 跳转到完善资料*/
			intent(PerfectInfoActivity.class);
			break;
		case R.id.rl_myquestion:
			/** 跳转到我的问答 */
			intent(MyQAnswerActivity.class);
			break;
		case R.id.rl_anonymous:
			/** 跳转到匿名八卦 */
			intent(MyAmActivity.class);
			break;
		case R.id.rl_collection:
			/** 跳转到我的收藏 */
			intent(MyCollectionActivity.class);
			break;
		case R.id.rl_sets:
			/** 跳转到设置 */
			intent(SettingActivity.class);
			break;
		default:
			break;
		}
		
	}
	
	/**
	 * 显示认证弹出框
	 */
	private void showAuthLevel(String msg) {
		dialog = new Dialog(getActivity(), R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.CENTER);
		View view = LayoutInflater.from(getActivity()).inflate(
				R.layout.custom_message_dialog, null);
		Button okBtn = (Button) view.findViewById(R.id.custom_msg_ok);
		Button cancelBtn = (Button) view.findViewById(R.id.custom_msg_cancel);
		cancelBtn.setVisibility(View.GONE);
		TextView message = (TextView) view
				.findViewById(R.id.custom_msg_message);
		message.setText(msg);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
	}
	
	
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}
	
	private void intent(Class cls) {
		Intent intent = new Intent();
		intent.setClass(getActivity(), cls);
		startActivity(intent);
	}

	@Override
	public void imageChange(String headCachePath) {
		Glide.with(this)
		.load(new File(headCachePath))
		.centerCrop()
		.placeholder(R.drawable.default_head_icon)
		.crossFade()
		.override(Utils.dip2px(getContext(), 40),Utils.dip2px(getContext(), 40))
		.bitmapTransform(new GlideCircleTransform(getContext()))
		.thumbnail(0.1f)
		.into(headIV);
//		headIV.setImageBitmap(bitmap);
	}

	@Override
	public void dataChanged(String nickName, String sex, String signate,
			String label) {
		prefectInfoRl.setVisibility(View.GONE);
		GlobalContext.label = label;
		labelTV.setText(label);
		signateTV.setText( "签名：" + signate);
		nickNameTV.setText(nickName);
	}
	public void onResume() {
	    super.onResume();
	    MobclickAgent.onPageStart("FourthFragment"); //统计页面，"MainScreen"为页面名称，可自定义
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPageEnd("FourthFragment"); 
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		/**	注释这行代码，防止界面跳转时，界面被回收	*/
//		super.onSaveInstanceState(outState);
	}
	
}